package com.itboye.mengwu.app.ui.mymeng;

import java.util.Observable;
import java.util.Observer;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itboye.config.GlobalConfig;
import com.itboye.core.alert.ByAlert;
import com.itboye.core.object.BaseActivity;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.core.utils.XImageLoader;
import com.itboye.mengwu.R;
import com.itboye.mengwu.data.model.OrderDetailBean;
import com.itboye.mengwu.data.presenter.OrderPresenter;

public class ActivityTuiHuo extends BaseActivity implements OnClickListener, Observer {
	TextView txt_title;
	ImageView back, goods_im;
	TextView goods_name, goods_price, huanhuo;
	EditText why, shuoming;
	Button tijiao;
	private OrderDetailBean orderDetailBean;
	LinearLayout goodsGroup;
	String reason, orderCode, uid, money, note;
	private OrderPresenter orderPresenter;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.activity_jinhuanhuo;
	}

	@Override
	public void onMyCreate() {
		// TODO Auto-generated method stub
		txt_title.setText("换货");
		uid = SPUtils.get(this, null, SpPublicParams.UID, "") + "";
		orderDetailBean = (OrderDetailBean) getIntent().getSerializableExtra(
				"model");
		orderCode = orderDetailBean.getOrderCode();
		for (int i = 0; i < orderDetailBean.getItems().size(); i++) {
			View view = LayoutInflater.from(ActivityTuiHuo.this).inflate(
					R.layout.shouhou_goodsitem, null);
			XImageLoader.load(GlobalConfig.API_URL + "/picture/index?id="
					+ orderDetailBean.getItems().get(i).getImg(),
					((ImageView) view.findViewById(R.id.goods_im)));
			((TextView) view.findViewById(R.id.goods_name))
					.setText(orderDetailBean.getItems().get(i).getName());
			((TextView) view.findViewById(R.id.goods_price)).setText("￥"
					+ orderDetailBean.getItems().get(i).getPrice());
			goodsGroup.addView(view);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		case R.id.tijiao:
			reason=why.getText().toString();
			money="0.01";
			note=shuoming.getText().toString();
			orderPresenter=new OrderPresenter(this);
			orderPresenter.shenQingShouHou(uid, orderCode, money, reason, "2",true,note);
			finish();
			break;
		default:
			break;
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == OrderPresenter.ShenQingSuccess) {
				ByAlert.alert(entity.getData() + "");
				finish();
			} else if (entity.getEventType() == OrderPresenter.ShenQingFail) {
				ByAlert.alert(entity.getData() + "");
				// finish();
			}
		}
	}

}
